/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.commons.tools;

import java.util.List;
import java.util.Locale;

import sk.jp.commons.utils.LocaleUtils;

/**
 * Adds support for initializing of <code>Messages</code> catalog by <i>Spring</i>.
 * <p>
 * Example of Spring configuration:<br>
 * <pre>
 * ...
 * &lt;bean id="sk.jp.commons.tools.messagesInitializer"
 *   class="MessagesInitializer"
 *   init-method="init"&gt;
 *   &lt;property name="locale" value="sk_EN"/&gt;
 *   &lt;property name="msgSrcPaths"&gt;
 *     &lt;list&gt;
 *       &lt;value&gt;sk.app.web.messages&lt;/value&gt;
 *       &lt;value&gt;sk.app.core.messages&lt;/value&gt;
 *     &lt;/list&gt;
 *   &lt;/property&gt;
 * &lt;/bean&gt;
 * </pre>
 *
 * @author Jan Pastor 
 */
public class MessagesInitializer {

    private Locale locale;
    private List<String> msgSrcPaths;

    //----------------------------------------------------------------------

    /**
     * Set locale used for loading message catalogs.
     * @param locale the entire locale, with the language, country and variant separated by
     * underbars. Language is always lower case, and country is always upper case.
     * If the language is missing, the string will begin with an underbar. If both
     * the language and country fields are missing, this function will return the
     * empty string, even if the variant field is filled in (you can't have a locale
     * with just a variant-- the variant must accompany a valid language or country
     * code). Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC"

     */
    public void setLocale(String locale) {
        this.locale = LocaleUtils.parseLocale(locale);
    }

    /**
     * Set message catalogs paths.
     * @param msgSrcPaths base name of properties files (package + name),
     *            without '.properties' extension.
     */
    public void setMsgSrcPaths(List<String> msgSrcPaths) {
        this.msgSrcPaths = msgSrcPaths;
    }

    /**
     * Initialize <code>Message</code> singleton using set properties (locale and
     * msgSrcPaths).
     */
    public void init(){
        Messages.init(msgSrcPaths, locale);
    }

}
